package com.wgyspring.common.core.pojo;

import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

@Data
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    private T data;
    private int code;
    private String msg;

    public R() {

    }
    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public R(ResultCode resultCode, T data) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
        this.data = data;
    }
    public R(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
    }

    public static <T> R<T> success(){
        return success(null);
    }
    public static <T> R<T> success(T data) {
        return new R<>(ResultCode.SUCCESS, data);
    }
    public static <T> R<T> success(T data,String msg) {
        return new R<>(ResultCode.SUCCESS.getCode(), msg,data);
    }

    public static <T> R<T> result(ResultCode resultCode,T data){
        return new R<>(resultCode,data);
    }

    public static <T> R<T> fail(){
        return fail(ResultCode.ERROR);
    }
    public static <T> R<T> fail(ResultCode resultCode){
        return new R<>(resultCode);
    }
    public static <T> R<T> fail(String msg){
        return new R<>(ResultCode.ERROR.getCode(),msg,null);

    }
}
